package com.tianyang.yukuaifuzs.module.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.View;

import com.tianyang.yukuaifuzs.R;
import com.tianyang.yukuaifuzs.common.utils.DateCheckPopListener;
import com.tianyang.yukuaifuzs.module.dialog.interfaces.IAchieveListener;
import com.tianyang.yukuaifuzs.module.roundview.RoundTextView;
import com.tianyang.yukuaifuzs.utils.DateUtils;
import com.tianyang.yukuaifuzs.utils.ToastUtil;
import com.tianyang.yukuaifuzs.utils.VerifyUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Description：业绩时间筛选
 * Created by xyj on 2024/3/25
 */
public class AchievementDialog extends Dialog {

    @BindView(R.id.startTimeRtv)
    RoundTextView startTimeRtv;
    @BindView(R.id.endTimeRtv)
    RoundTextView endTimeRtv;

    private Context mContext;

    private DateCheckPopListener mDateStartPopListener;

    private DateCheckPopListener mDateEndPopListener;

    private IAchieveListener listener;

    public void setListener(IAchieveListener listener) {
        this.listener = listener;
    }

    public AchievementDialog(Context context) {
        super(context, R.style.top_pop_menu);
        this.mContext = context;
        setContentView(R.layout.dialog_achievement);
        ButterKnife.bind(this);
        setCancelable(false);
        String startTime = DateUtils.getTimeAgo(30, "yyyy-MM-dd");
        String endTime = DateUtils.getCurrentTime("yyyy-MM-dd");
        startTimeRtv.setText(startTime);
        endTimeRtv.setText(endTime);
    }

    @OnClick({R.id.confirmRtv, R.id.resetRtv, R.id.startTimeRtv, R.id.endTimeRtv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.confirmRtv:
                String startDateStr = startTimeRtv.getText().toString();
                String endDateStr = endTimeRtv.getText().toString();
                if (DateUtils.getDifferenceTime(startDateStr, endDateStr) < 0) {
                    ToastUtil.show("查询结束日期必须大于开始日期");
                    return;
                }
                if(listener != null) {
                    listener.callback(startDateStr, endDateStr);
                }
                dismiss();
                break;
            case R.id.resetRtv:
                dismiss();
                break;
            case R.id.startTimeRtv:
                if (mDateStartPopListener == null) {
                    mDateStartPopListener = new DateCheckPopListener((Activity) mContext, true);
                }
                mDateStartPopListener.handleRegionCheck(view, "请选择开始日期", startTimeRtv);
                break;
            case R.id.endTimeRtv:
                if (mDateEndPopListener == null) {
                    mDateEndPopListener = new DateCheckPopListener((Activity) mContext, true);
                }
                mDateStartPopListener.handleRegionCheck(view, "请选择结束日期", endTimeRtv);
                break;
        }
    }

    public void setTimeData(String startTime, String endTime) {
        startTimeRtv.setText(VerifyUtil.checkString(startTime));
        endTimeRtv.setText(VerifyUtil.checkString(endTime));
    }
}
